<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs2.model.*, bbs2.dao.*, bbs2.service.*, bbs2.util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="../_checkUser.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="../_header.jsp" %>  
<style>
    form.comment { border-bottom: 1px dashed #ddd; padding: 10px; margin: 0; }
    form.comment > div:nth-of-type(1) { color: #aaa; font-size: 9pt; margin-bottom: 8px; }
    form.comment > div:nth-of-type(1) span { margin-right: 30px; }
    form.comment > div:nth-of-type(2) { white-space: pre-wrap; }
    textarea { ime-mode: active; width: 90%; min-height: 100px; }
    .edit, .add { display: none; }
    div.container { margin-bottom: 100px; }
</style>
</head>
<%
    RequestParameter param = new RequestParameter(request);
    int targetType = param.getInt("targetType", 0);
    int targetId = param.getInt("targetId", 0);
    String cmd = param.getString("cmd", "");
    int id = param.getInt("id", 0);
    String body = param.getString("body", "");
    String errMsg = null;
    
    int userId = UserService.getCurrentUser(session).getId();
    boolean isAdmin = UserService.isCurrentUserAdmin(session);
    
    if (cmd.equals("save")) {
        if (id == 0)
            CommentDAO.insert(userId, targetType, targetId, body);
        else
            CommentDAO.update(id, body);
    } else if (cmd.equals("delete"))
        CommentDAO.delete(id);
    
    ArrayList<Comment> list = CommentDAO.select(targetType, targetId); 
    
    String url = "/bbs2/comment/list.jsp?targetType=" + targetType + "&targetId=" + targetId;            
%>
<script>
    function resize_parent_iframe() {
        if (window.parent) {
            var h = $("html").height();
            $('iframe.comment', window.parent.document).css("height", h);
        }
    }
    
    function readMode() {
        $(".read").show();
        $(".edit").hide();
        $(".add").hide();
    }
    
    function editMode(btn) {
        $(btn).parents("form").find(".read").hide();
        $(btn).parents("form").find(".edit").show();
        resize_parent_iframe();
    }
    
    function addMode() {
        $("a.read").hide();
        $(".add").show();
        resize_parent_iframe();
    }    

    $(resize_parent_iframe);
</script>
<body>
<div class="container">

    <% for (Comment comment : list) { %>    
        <form class="comment" method="post">
            <input type="hidden" name="id" value="<%= comment.getId() %>">
            <div class="read">
                <span><%= comment.getName() %></span>
                <span><%= DateUtil.toYMDHMS(comment.getWriteTime()) %></span>
                <% if (isAdmin || comment.getUserId() == userId) { %>
                    <a class="myMiniButton" onclick="editMode(this)">편집</a>
                    <button type="submit" class="myMiniButton" name="cmd" value="delete" 
                            onclick="return confirm('삭제하시겠습니까?')">삭제</button>
                <% } %>
            </div>
            <div class="read"><%= comment.getBody() %></div>
            <div class="edit">
                <textarea name="body"><%= comment.getBody() %></textarea>
                <div>
                    <button type="submit" class="btn btn-small" name="cmd" value="save">저장</button>
                    <a class="btn btn-small" onclick="readMode()">취소</a>
                </div>
            </div>
        </form>    
    <% } %>
    
    <div class="controls pull-right">
        <a onclick="addMode()" class="btn btn-small read">덧글 작성</a>
    </div>
    
    <form method="post" class="add">
        <textarea name="body"></textarea>
        <div>
            <button type="submit" class="btn btn-small" name="cmd" value="save">저장</button>
            <a class="btn btn-small" onclick="readMode()">취소</a>
        </div>
    </form>
    
</div>
</body>
